<?php
/**
 * Created by PhpStorm.
 * User: jswei
 * Date: 2018/4/24
 * Time: 8:54
 */

namespace app\admin\model;

use app\index\model\Areas;
use app\index\model\Cities;
use app\index\model\Provinces;
use think\Db;
use think\Model;

class Article extends Model{
    protected $pk = 'id';
    protected $autoWriteTimestamp = true;

    /**
     * @param array $where
     * @param int $pageSize
     * @param string $field
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList($where= [], $pageSize=10,$field='*'){
        $list = $this
            ->with('mch')
            ->where($where)
            ->field($field)
            ->order('create_time desc')
            ->paginate($pageSize);
        foreach ($list as $k => &$v){
            $v['location'] = $this->getLocation($v['id']);
        }
        return $list;
    }

     public function mch(){
        return $this->hasOne('Mch','id','mcid');
     }

    /**
     * @param int $id
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
     protected function getLocation($id=0){
        $info = $this->find($id);
        $_info = db('provinces')->where('provinceid','eq',$info['pro'])->find();
        $info['pro'] =$_info['province'];
        $_info = db('cities')->where('cityid','eq',$info['city'])->find();
        $info['city'] =$_info['city'];
        $_info = db('areas')->where('areaid','eq',$info['area'])->find();
        $info['area'] =$_info['area'];
        if($info){
            if($info['pro'] && $info['city'] && $info['area'] && $info['address']){
                return "{$info['pro']},{$info['city']},{$info['city']},{$info['address']}";
            }elseif($info['pro'] && $info['city'] && $info['area']){
                return "{$info['pro']},{$info['city']},{$info['area']}";
            }elseif($info['pro'] && $info['city']){
                return "{$info['pro']},{$info['city']}";
            }elseif($info['pro']){
                return "{$info['pro']}";
            }else{
                return "全国";
            }
        }else{
            return '';
        }
     }

    public function getProAttr1($value){
        if($value){
            $pro = new Provinces();
            $info = $pro->where('provinceid','eq',$value)->find();
            return $info['province'];
        }else{
            return '';
        }
    }

    public function getCityAttr1($value){
        if($value){
            $pro = new Cities();
            $info = $pro->where('cityid','eq',$value)->find();
            return $info['city'];
        }else{
            return '';
        }
    }

    public function getQrcodeAttr($value){
        return $value ? $value : '';
    }

    public function getAreaAttr1($value){
        if($value){
            $pro = new Areas();
            $info = $pro->where('areaid','eq',$value)->find();
            return $info['area'];
        }else{
            return '';
        }
    }

    /**
     * 添加文章
     * @param $data
     * @param int $mc_id
     * @param string $out
     * @return bool|int|string
     */
    public function addNew($data,$mc_id=0,&$out=''){
         if($mc_id){
             $info = (new Mch())->get($mc_id);
             if($info['money']==0){
                 $out= '账户余额不足请先充值';
                 return false;
             }
             if($this->insert($data)){
                 $out = '添加失败';
                 return false;
             }
             return true;
         }else{
             if($this->insert($data)){
                 $out = '添加失败';
                 return false;
             }
             return true;
         }
    }

    /**
     * 活动截止时间
     * @param $value
     * @return false|string
     */
    public function getFinishTimeAttr($value){
         return $value?date('Y-m-d H:i:s',$value):'';
    }

    /**
     * 获取生成二维码价格
     * @param int $type
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function generateQrcodePrice($type=0){
        $config = (new Setting())->getDetail();
        if($type==1){
            return $config['pro'];
        }elseif($type==2){
            return $config['city'];
        }elseif($type==3){
            return $config['area'];
        }elseif($type==4){
            return $config['town'];
        }else{
            return $config['country'];
        }
    }

    /**
     *
     * @param int $id
     * @param string $out
     * @return bool
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function destory($id=0,&$out=''){
        $info = $this->get($id);
        if(!$info){
            $out = '活动不存在';
            return false;
        }
        $lott = new Lottery();
        $money = $lott->getMoneyByAid($id);
        Db::startTrans();
        if($money){
            $mch = new Mch();
            $_mch = $mch->get($info['mcid']);
            if(!$_mch){
                $out = '商户不存在';
                return false;
            }
            $name='删除活动|'.$info['title'].'|抽奖余额返还';
            $result = $mch->dec_hbconsumer($name,$id,$_mch['id'],$money,$out1);
            if(!$result) {
                $out = $out1;
                return false;
            }
            $result1 = $this->where('id','eq',$id)->setField('status',-1);
            if($result && $result1){
                Db::commit();
                return true;
            }else{
                Db::rollback();
                $out = '操作失败';
                return false;
            }
        }else{
            $result = $this->where('id','eq',$id)->setField('status',-1);
            if($result){
                Db::commit();
                return true;
            }else{
                Db::rollback();
                $out = '操作失败';
                return false;
            }
        }
    }

    /**
     * 获取商户的活动
     * @param $id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getIdsByMcid($id){
        $list = $this
            ->field('id')
            ->where('mcid','eq',$id)
            ->select();
        $ids = [];
        foreach ($list as $v){
            array_push($ids,$v['id']);
        }
        return $ids;
    }

    protected function getFailInfoAttr($value){
        return $value?$value:'--';
    }
}